package com.spic.business.plan.drill.controller;

import com.spic.business.plan.drill.domain.PlanDrillPerformInfo;
import com.spic.business.plan.drill.service.IPlanDrillPerformInfoService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 应急预案演练与培训Controller
 * 
 * @author renxiang
 * @date 2021-10-29
 */
@RestController
@RequestMapping("/plan/drillPerform")
public class PlanDrillPerformInfoController extends BaseController
{
    @Autowired
    private IPlanDrillPerformInfoService planDrillPerformInfoService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询应急预案演练与培训列表
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:list")
    @GetMapping("/list")
    public TableDataInfo list(PlanDrillPerformInfo planDrillPerformInfo) {
        // 获取部门及子部门
        if (StringUtils.isNotEmpty(planDrillPerformInfo.getOrganizeDepartment())) {
            List<Long> organizeDepartments = remoteDeptService.getDeptAndChildren(Long.parseLong(planDrillPerformInfo.getOrganizeDepartment())).getData();
            planDrillPerformInfo.setOrganizeDepartments(organizeDepartments);
        }
        startPage();
        List<PlanDrillPerformInfo> list = planDrillPerformInfoService.selectPlanDrillPerformInfoList(planDrillPerformInfo);
        return getDataTable(list);
    }

    /**
     * 导出应急预案演练与培训列表
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:export")
    @Log(title = "应急预案演练与培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanDrillPerformInfo planDrillPerformInfo) throws IOException
    {
        List<PlanDrillPerformInfo> list = planDrillPerformInfoService.selectPlanDrillPerformInfoList(planDrillPerformInfo);
        ExcelUtil<PlanDrillPerformInfo> util = new ExcelUtil<PlanDrillPerformInfo>(PlanDrillPerformInfo.class);
        util.exportExcel(response, list, "应急预案演练与培训数据");
    }

    /**
     * 获取应急预案演练与培训详细信息
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(planDrillPerformInfoService.selectPlanDrillPerformInfoById(id));
    }

    /**
     * 新增应急预案演练与培训
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:add")
    @Log(title = "应急预案演练与培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlanDrillPerformInfo planDrillPerformInfo)
    {
        return AjaxResult.success(planDrillPerformInfoService.insertPlanDrillPerformInfo(planDrillPerformInfo));
    }

    /**
     * 修改应急预案演练与培训
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:edit")
    @Log(title = "应急预案演练与培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlanDrillPerformInfo planDrillPerformInfo)
    {
        return toAjax(planDrillPerformInfoService.updatePlanDrillPerformInfo(planDrillPerformInfo));
    }

    /**
     * 删除应急预案演练与培训
     */
    @PreAuthorize(hasPermi = "business/plan:drillPerform:remove")
    @Log(title = "应急预案演练与培训", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(planDrillPerformInfoService.deletePlanDrillPerformInfoByIds(ids));
    }


}
